﻿' (C) Copyright 2010 by  
'
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.Windows
Imports Autodesk.AutoCAD.Windows
Imports System.Windows.Forms
Imports System.Security.Permissions.SecurityPermission

'import for button icons
Imports System.Drawing
Imports System.IO
'Imports System.Media
Imports System.Windows.Media

' This line is not mandatory, but improves loading performances
<Assembly: ExtensionApplication(GetType(AutoCAD_VB_plug_in1.MyPlugin))> 

Namespace AutoCAD_VB_plug_in1

    ' This class is instantiated by AutoCAD once and kept alive for the 
    ' duration of the session. If you don't do any one time initialization 
    ' then you should remove this class.
    Public Class MyPlugin
        Implements IExtensionApplication

        



        Public Sub Initialize() Implements IExtensionApplication.Initialize

            StartRibbon()

        End Sub

        Public Sub Terminate() Implements IExtensionApplication.Terminate
            ' Do plug-in application clean up here
        End Sub


        Private Sub StartRibbon()
            Dim ribCntrl As RibbonControl = ComponentManager.Ribbon
            Dim ribTab As New RibbonTab()
            ribTab.Title = "Aras Innovator"
            ribTab.Id = "Aras"
            ribCntrl.Tabs.Add(ribTab)
            ribTab.IsActive = True
            addContent(ribTab)
        End Sub

        Private Sub addContent(ByVal ribTab As RibbonTab)



            'create the panel source
            Dim ribSourcePanel_Status As RibbonPanelSource = New RibbonPanelSource()
            Dim ribSourcePanel_Settings As RibbonPanelSource = New RibbonPanelSource()
            Dim ribSourcePanel_LockOp As RibbonPanelSource = New RibbonPanelSource()
            Dim ribSourcePanel_DocInfo As RibbonPanelSource = New RibbonPanelSource()
            Dim ribSourcePanel_help As RibbonPanelSource = New RibbonPanelSource()
            Dim ribSourcePanel_Files As RibbonPanelSource = New RibbonPanelSource()
            Dim ribSourcePanel_Blocs As RibbonPanelSource = New RibbonPanelSource()
            'name panels
            ribSourcePanel_Status.Title = "Status"
            ribSourcePanel_Settings.Title = "Settings"
            ribSourcePanel_LockOp.Title = "Commands"
            ribSourcePanel_DocInfo.Title = "Doc Info"
            ribSourcePanel_help.Title = "Help"
            ribSourcePanel_Files.Title = "Files"
            ribSourcePanel_Blocs.Title = "Blocks"


            'now declare the panels
            Dim ribPanel_Status As New RibbonPanel()
            Dim ribPanel_Settings As New RibbonPanel()
            Dim ribPanel_LockOp As New RibbonPanel()
            Dim ribPanel_DocInfo As New RibbonPanel()
            Dim ribPanel_Help As New RibbonPanel()
            Dim ribPanel_Files As New RibbonPanel()
            Dim ribPanel_Blocs As New RibbonPanel()


            '**** ADD THE PANELS TO THE RIBBON ****

            ' add Status Panel
            ribPanel_Status.Source = ribSourcePanel_Status
            ribTab.Panels.Add(ribPanel_Status)
            ' add Files Panel
            ribPanel_Files.Source = ribSourcePanel_Files
            ribTab.Panels.Add(ribPanel_Files)
            ' add Blocs Panel
            ribPanel_Blocs.Source = ribSourcePanel_Blocs
            ribTab.Panels.Add(ribPanel_Blocs)
            ' add LockOp Panel
            ribPanel_LockOp.Source = ribSourcePanel_LockOp
            ribTab.Panels.Add(ribPanel_LockOp)
            ' add DocInfo Panel
            ribPanel_DocInfo.Source = ribSourcePanel_DocInfo
            ribTab.Panels.Add(ribPanel_DocInfo)
            ' add Help Panel
            ribPanel_Help.Source = ribSourcePanel_help
            ribTab.Panels.Add(ribPanel_Help)
            ' add Settings Panel
            ribPanel_Settings.Source = ribSourcePanel_Settings
            ribTab.Panels.Add(ribPanel_Settings)


            'declare som objects 
            Dim riblabel As RibbonLabel
            Dim ribrow As RibbonRowBreak
            Dim ribButton As RibbonButton
            Dim ribBar As RibbonSeparator

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '                             populate Status Panel                               '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'Button Browse Blocs
            ribButton = New RibbonButton()

            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Text = "Connection"
                .Id = "Connection"
                .CommandParameter = "Connection"
                .ShowText = True
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.connection32)
            End With
            ribSourcePanel_Status.Items.Add(ribButton)


            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '                             populate File Panel                               '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'Button Browse Files
            ribButton = New RibbonButton()

            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Text = "Browse"
                .Id = "Browse"
                .CommandParameter = "BrowseFiles"
                .ShowText = True
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.browsefiles32)
                .IsEnabled = True
            End With
            ribSourcePanel_Files.Items.Add(ribButton)

            'Button add Files
            ribButton = New RibbonButton()

            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Text = "Add File"
                .Id = "Add File"
                .CommandParameter = "AddFile"
                .ShowText = True
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.AddFile32)
                .IsEnabled = True
            End With
            ribSourcePanel_Files.Items.Add(ribButton)

           

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '                             populate Blocs Panel                               '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'Button Browse Blocs
            ribButton = New RibbonButton()

            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Text = "Browse"
                .Id = "Browse"
                .CommandParameter = "BrowseBlocs"
                .ShowText = True
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.blocks32)
                .IsEnabled = True
            End With
            ribSourcePanel_Blocs.Items.Add(ribButton)

            'Button add Blocs
            ribButton = New RibbonButton()

            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Text = "Add Block"
                .Id = "Add Block"
                .CommandParameter = "AddBlock"
                .ShowText = True
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.addblock32)
                .IsEnabled = True
            End With
            ribSourcePanel_Blocs.Items.Add(ribButton)

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '                           populate LockOp Panel                               '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'Button Lock 

            ribButton = New RibbonButton()
            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Text = "Lock"
                .CommandParameter = "Lock"
                .Id = "Lock"
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.Lock32)
                .ShowText = True
                .IsEnabled = False
            End With
            ribSourcePanel_LockOp.Items.Add(ribButton)


            'Button Save 
            ribButton = New RibbonButton()
            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Text = "Save"
                .CommandParameter = "Save"
                .ShowText = True
                .Id = "Save"
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.Save32)
                .IsEnabled = False
            End With
            ribSourcePanel_LockOp.Items.Add(ribButton)


            'Button UnLock 
            ribButton = New RibbonButton()
            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Text = "Unlock"
                .Id = "Unlock"
                .CommandParameter = "Unlock"
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.Unlock32)
                .ShowText = True
                .IsEnabled = False
            End With
            ribSourcePanel_LockOp.Items.Add(ribButton)

            'Button Exit
            ribButton = New RibbonButton()
            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Text = "Exit"
                .Id = "Exit"
                .CommandParameter = "Exit"
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.Exit32)
                .ShowText = True
            End With
            ribSourcePanel_LockOp.Items.Add(ribButton)



            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '                          populate DocInfo Panel                               '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


            'Ajout du block d'informations
            Dim ribrowpanel = New RibbonRowPanel()
            ribrow = New RibbonRowBreak
            riblabel = New RibbonLabel()
            riblabel.Text = "Name: XXXXXXXXXXXXXXX"
            riblabel.Id = "Name"
            riblabel.Image = LoadImage(My.Resources.Resource1.docname16)
            ribrowpanel.Items.Add(riblabel)
            'retour à la ligne
            ribrowpanel.Items.Add(ribrow)

            riblabel = New RibbonLabel()
            riblabel.Name = "Info_Class"
            riblabel.Text = "Class: XXXXXXXXXXXXXXX"
            riblabel.Id = "Class"
            riblabel.Image = LoadImage(My.Resources.Resource1.class16)
            ribrowpanel.Items.Add(riblabel)
            'retour à la ligne
            ribrowpanel.Items.Add(ribrow)

            riblabel = New RibbonLabel()
            riblabel.Name = "Info_DocNum"
            riblabel.Text = "Doc Number: XXXXXXXXXXXXXXX"
            riblabel.Id = "DocNum"
            riblabel.Image = LoadImage(My.Resources.Resource1.DocNumber16)
            ribrowpanel.Items.Add(riblabel)

            ribSourcePanel_DocInfo.Items.Add(ribrowpanel)

            ribBar = New RibbonSeparator()
            ribSourcePanel_DocInfo.Items.Add(ribBar)

            ribrowpanel = New RibbonRowPanel()

            riblabel = New RibbonLabel()
            riblabel.Name = "Info_Version"
            riblabel.Text = "Version: XXXXX"
            riblabel.Id = "Version"
            riblabel.Image = LoadImage(My.Resources.Resource1.version16)
            ribrowpanel.Items.Add(riblabel)
            'retour à la ligne
            ribrowpanel.Items.Add(ribrow)

            riblabel = New RibbonLabel()
            riblabel.Name = "Info_State"
            riblabel.Text = "State: XXXXXXXXXX"
            riblabel.Id = "State"
            riblabel.Image = LoadImage(My.Resources.Resource1.status16)
            ribrowpanel.Items.Add(riblabel)
            'retour à la ligne
            ribrowpanel.Items.Add(ribrow)

            riblabel = New RibbonLabel()
            riblabel.Name = "Info_Generation"
            riblabel.Text = "Generation: XXXXX"
            riblabel.Id = "Generation"
            riblabel.Image = LoadImage(My.Resources.Resource1.generation16)
            ribrowpanel.Items.Add(riblabel)


            ' ajout du 
            ribSourcePanel_DocInfo.Items.Add(ribrowpanel)

            ribBar = New RibbonSeparator()
            ribSourcePanel_DocInfo.Items.Add(ribBar)

            ribrowpanel = New RibbonRowPanel()
            ribButton = New RibbonButton()
            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Id = "Rights"
                .Text = "Rights"
                .CommandParameter = "Rights"
                .ShowText = True
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.rights32)
            End With
            ribrowpanel.Items.Add(ribButton)
            ribSourcePanel_DocInfo.Items.Add(ribrowpanel)


            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '                             populate Help Panel                               '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ribrowpanel = New RibbonRowPanel()
            'Button Get Connection settings
            ribButton = New RibbonButton()
            With ribButton
                .Size = RibbonItemSize.Standard
                .Orientation = Windows.Controls.Orientation.Horizontal
                .Id = "Help"
                .Text = "Help"
                .CommandParameter = "Help"
                .ShowText = True
                .CommandHandler = New AdskCommandHandler()
                .Image = LoadImage(My.Resources.Resource1.help16)
            End With
            ribrowpanel.Items.Add(ribButton)
            'retour à la ligne
            ribrowpanel.Items.Add(ribrow)
            'Button Get Connection settings
            ribButton = New RibbonButton()
            With ribButton
                .Size = RibbonItemSize.Standard
                .Orientation = Windows.Controls.Orientation.Horizontal
                .Id = "About"
                .Text = "About"
                .CommandParameter = "About"
                .ShowText = True
                .CommandHandler = New AdskCommandHandler()
                .Image = LoadImage(My.Resources.Resource1.about16)
            End With
            ribrowpanel.Items.Add(ribButton)
            ribrowpanel.Width = 500
            ribSourcePanel_help.Items.Add(ribrowpanel)

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '                           populate Settings Panel                             '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


            'Button Get Connection settings
            ribButton = New RibbonButton()
            With ribButton
                .Size = RibbonItemSize.Large
                .Orientation = Windows.Controls.Orientation.Vertical
                .Id = "Settings"
                .Text = "Settings"
                .CommandParameter = "Settings"
                .ShowText = True
                .CommandHandler = New AdskCommandHandler()
                .LargeImage = LoadImage(My.Resources.Resource1.Settings32)
            End With
            ribSourcePanel_Settings.Items.Add(ribButton)

            'add hiden item number
            riblabel = New RibbonLabel()
            With riblabel
                .Name = "ItemNumber"
                .Id = "ItemNumber"
                .Text = "ItemNumber"
                .IsVisible = False
            End With
            ribSourcePanel_Settings.Items.Add(riblabel)

            'add hiden item number
            Dim ribTB = New RibbonTextBox()
            With ribTB
                .Name = "UserName"
                .Id = "UserName"
                .Text = "UserName"
                .IsVisible = False
                .Image = LoadImage(My.Resources.Resource1.user16)
            End With
            ribSourcePanel_Settings.Items.Add(ribTB)


        End Sub

        Private Shared Function LoadImage(ByVal ImageRes As Bitmap) As System.Windows.Media.Imaging.BitmapImage
            Dim pic As Bitmap = ImageRes
            Dim ms As New MemoryStream()
            pic.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
            Dim bi As New System.Windows.Media.Imaging.BitmapImage()
            bi.BeginInit()
            bi.StreamSource = ms
            bi.EndInit()
            Return bi
        End Function

    End Class



    Public Class AdskCommandHandler



        Implements System.Windows.Input.ICommand


        Dim ArasUrl As String
        Dim ArasDB As String
        Dim ArasUser As String
        Dim ArasPass As String
        Dim UserLocalDir As String
        Dim DocumentName As String
        Dim DocumentNum As String
        Dim ItemNumber As String
        Dim ribCntrl As RibbonControl = ComponentManager.Ribbon
        Dim ribtab As RibbonTab = ribCntrl.FindTab("Aras")

        Dim ActiveDoc As Autodesk.AutoCAD.Interop.AcadDocument

        Dim PtConfigs As Autodesk.AutoCAD.Interop.Common.AcadPlotConfigurations
        Dim PlotConfig As Autodesk.AutoCAD.Interop.Common.AcadPlotConfiguration

        Dim PtObj As Autodesk.AutoCAD.Interop.AcadPlot


        Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
            Return True
        End Function

        Public Event CanExecuteChanged As EventHandler Implements System.Windows.Input.ICommand.CanExecuteChanged

        Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute

            Dim ribBtn As RibbonButton = TryCast(parameter, RibbonButton)
            Dim ribcombo As RibbonCombo = TryCast(parameter, RibbonCombo)
            Dim Thumbnail_CreationResult As Boolean
            If ribBtn IsNot Nothing Then

                Dim folder As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
                ActiveDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.AcadDocument
                DocumentName = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.UserData("Document name")
                UserLocalDir = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.UserData("UserLocalDir")

                ribtab.FindItem("Name", True).Text = "Doc Name:" & DocumentName

                Select Case ribBtn.CommandParameter

                    

                    Case "Settings"
                        Dim FormulaireSettings As New LoginForm1
                        FormulaireSettings.Show()

                    Case "BrowseFiles"
                        Dim FormulaireLogin As New FileBrowser
                        FormulaireLogin.Show()

                    Case "AddFile"
                        Dim FormulaireLogin As New AddBlock
                        FormulaireLogin.Show()

                    Case "BrowseBlocs"
                        Dim FormulaireBrowseBlocs As New BlocsBrowser
                        FormulaireBrowseBlocs.Show()

                    Case "AddBlock"
                        Dim FormulaireLogin As New AddBlock
                        FormulaireLogin.Show()

                    Case "GetInfo"
                        Dim GetParam As New ComINI
                        Dim username As String
                        username = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUN")
                        If username = "" Then
                            username = "user not set"
                        End If
                        ribtab.FindItem("UserName", True).Text = username
                        Dim FormDoc As New FileBrowser
                        FormDoc.Show()

                        ribtab.FindItem("DocNum", True).Text = ""
                        ribtab.FindItem("Version", True).Text = ""
                        ribtab.FindItem("State", True).Text = ""
                        ribtab.FindItem("Name", True).Text = ""
                        ribtab.FindItem("Class", True).Text = ""
                        ribtab.FindItem("Generation", True).Text = ""
                        'Get_Doc_Informations()

                    Case "Save"

                        ItemNumber = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.UserData("Document ID")
                        ribtab.FindItem("DocNum", True).Text = "Doc Number:" & ItemNumber
                        Dim result As String
                        'uploader la nouvelle version du fichier
                        Dim GetParam As New ComINI

                        ArasUrl = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASURL")
                        ArasDB = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASDB")
                        ArasUser = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUN")
                        ArasPass = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUP")
                        Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
                        Dim FilepathComplete As String = UserLocalDir + "\" + DocumentName
                        Dim SaveSucceed As Boolean = False

                        Try

                            ActiveDoc.SaveAs(FilepathComplete)
                            SaveSucceed = True

                        Catch ex As System.Exception
                            MsgBox("Saving Failed because it might be opened in read-only mode")
                        End Try

                        If SaveSucceed = True Then

                            'Check if document has already a file attached
                            Dim hasfile As Boolean
                            hasfile = ConnecAras.DocHasFileTest(ItemNumber)

                            'create the thumbnail
                            Thumbnail_CreationResult = CreatePNG_Thumbnail()
                            If Thumbnail_CreationResult = False Then
                                MsgBox("error while creating thumbnail")
                            End If

                            'upload files
                            If hasfile = True Then
                                result = ConnecAras.UploadUpdatedDoc(ItemNumber, _
                                                                     FilepathComplete, _
                                                                     DocumentName, _
                                                                     False, _
                                                                     Thumbnail_CreationResult, _
                                                                     True)
                            Else
                                result = ConnecAras.UploadNewDoc(ItemNumber, _
                                                                 FilepathComplete, _
                                                                 DocumentName, _
                                                                 False, _
                                                                 Thumbnail_CreationResult, _
                                                                 True)
                            End If


                            MsgBox(result)

                            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.UserData("Document ID") = result
                            ItemNumber = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.UserData("Document ID")
                            ribtab.FindItem("DocNum", True).Text = "Doc Number:" & ItemNumber

                        End If

                    Case "Lock"


                        ItemNumber = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.UserData("Document ID")
                        ribtab.FindItem("DocNum", True).Text = "Doc Number:" & ItemNumber
                        ' lock du document, desactive le bouton lock et active le bouton unlock
                        Dim result As String
                        Dim GetParam As New ComINI
                        ArasUrl = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASURL")
                        ArasDB = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASDB")
                        ArasUser = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUN")
                        ArasPass = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUP")
                        Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
                        result = ConnecAras.LockDoc(ItemNumber)
                        MsgBox(result)
                        If result <> "error" Then
                            ribtab.FindItem("Unlock", True).IsEnabled = True
                            ribtab.FindItem("Save", True).IsEnabled = True
                            ribtab.FindItem("Lock", True).IsEnabled = False
                            DocumentNum = result
                            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.UserData("Document ID") = result
                        End If


                    Case "Unlock"


                        ItemNumber = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.UserData("Document ID")
                        ribtab.FindItem("DocNum", True).Text = "Doc Number:" & ItemNumber
                        Dim GetParam As New ComINI
                        ArasUrl = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASURL")
                        ArasDB = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASDB")
                        ArasUser = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUN")
                        ArasPass = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUP")
                        'verifier si le document est sauvegardé
                        Dim DocIsSaved As Boolean = False
                        Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
                        Dim result As String
                        Dim NewItemID As String
                        NewItemID = ItemNumber

                        Dim Pdf_or_dwf As Boolean = False
                        ribcombo = ribtab.FindItem("Visualisation", True)
                        If ribcombo.Current.id = "PDF" Then
                            Pdf_or_dwf = True
                        End If

                        'verifier si le document a déjà un fichier
                        Dim DocHasFile As Boolean
                        If DocIsSaved = True Then
                            DocHasFile = True
                        Else
                            DocHasFile = ConnecAras.DocHasFileTest(ItemNumber)
                        End If

                        'si non sauvegardé proposition de sauvegarder

                        If (DocIsSaved = False) Then
                            Dim QuestionResult As Integer
                            QuestionResult = MsgBox("Do you want to Save this version before unlocking the document?", MsgBoxStyle.YesNoCancel, "confirm saving the doc")
                            If QuestionResult = DialogResult.Yes Then
                                ' save + upload + unlock
                                'créer un pdf du fichier ACAD
                                Dim PdfCreationResult As Boolean
                                If Pdf_or_dwf = True Then

                                    PdfCreationResult = createpdf2()
                                Else
                                    PdfCreationResult = CreateDWF()
                                End If


                                Dim FilepathComplete As String = UserLocalDir + "\" + DocumentName

                                If DocHasFile = True Then
                                    ActiveDoc.SaveAs(FilepathComplete)
                                    NewItemID = ConnecAras.UploadUpdatedDoc(ItemNumber, _
                                                                            FilepathComplete, _
                                                                            DocumentName, _
                                                                            PdfCreationResult, _
                                                                            Thumbnail_CreationResult, _
                                                                            Pdf_or_dwf)
                                    ItemNumber = NewItemID
                                    NewItemID = ConnecAras.UnlockDoc(ItemNumber)

                                Else
                                    ActiveDoc.SaveAs(FilepathComplete)
                                    NewItemID = ConnecAras.UploadNewDoc(ItemNumber, _
                                                                        FilepathComplete, _
                                                                        DocumentName, _
                                                                        PdfCreationResult, _
                                                                        Thumbnail_CreationResult, _
                                                                        Pdf_or_dwf)
                                    NewItemID = ConnecAras.UnlockDoc(ItemNumber)

                                End If
                                'MsgBox(result)
                                ItemNumber = NewItemID
                                MsgBox(NewItemID)
                                ribtab.FindItem("Lock", True).IsEnabled = True
                                ribtab.FindItem("Save", True).IsEnabled = False
                                ribtab.FindItem("Unlock", True).IsEnabled = False

                            ElseIf QuestionResult = DialogResult.No Then
                                result = ConnecAras.UnlockDoc(ItemNumber)
                                MsgBox(result)
                                If result <> "error" Then
                                    ribtab.FindItem("Lock", True).IsEnabled = True
                                    ribtab.FindItem("Save", True).IsEnabled = False
                                    ribtab.FindItem("Unlock", True).IsEnabled = False
                                End If

                            Else
                                ' cancel

                            End If

                        End If
                    Case "Exit"
                        MsgBox(ribBtn.CommandParameter)

                    Case Else
                        MsgBox("Unknown Command")

                End Select

            End If
        End Sub
        Public Sub Get_Doc_Informations()


            ActiveDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.AcadDocument

            Dim GetParam As New ComINI
            Dim folder As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
            'MsgBox(ActiveDoc.Variables.Count)

            If (ActiveDoc.SummaryInfo.Title <> "") Then
                'split comments
                Dim data As String()
                data = Split(ActiveDoc.SummaryInfo.Comments, ";")
                Dim DocumentNameRecup As String = Split(data(2), "|")(1)
                Dim DocumentNumRecup As String = Split(data(0), "|")(1)
                Dim DocumentPathRecup As String = data(1)
                Dim DocumentClassRecup As String = Split(data(3), "|")(1)
                Dim ArasURLRecup As String = Split(data(4), "|")(1)
                Dim ArasDBRecup As String = Split(data(5), "|")(1)

                ribtab.FindItem("ItemNumber", True).Text = ActiveDoc.SummaryInfo.Title
                ribtab.FindItem("Name", True).Text = "Name :" + DocumentNameRecup
                ribtab.FindItem("DocNum", True).Text = "Doc Number :" + DocumentNumRecup
                If DocumentClassRecup <> "" Then
                    ribtab.FindItem("Class", True).Text = "Class :" + DocumentClassRecup
                Else
                    ribtab.FindItem("Class", True).Text = "Class :" + "No Class"
                End If


                ArasUrl = ArasURLRecup
                ArasDB = ArasDBRecup
                ActiveDoc.SummaryInfo.HyperlinkBase = ArasUrl
                ActiveDoc.SummaryInfo.Author = ArasDB
                UserLocalDir = DocumentPathRecup
                ActiveDoc.SummaryInfo.Subject = UserLocalDir
                DocumentName = DocumentNameRecup
                DocumentNum = DocumentNumRecup
                ArasUser = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "User")
                ArasPass = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "Pass")

                'test if doc is locked
                Dim DocIsLocked As Boolean
                If (ArasUser <> "") And (ArasPass <> "") Then
                    Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
                    DocIsLocked = ConnecAras.IsDocLocked(ActiveDoc.SummaryInfo.Title)
                    If DocIsLocked = True Then
                        ribtab.FindItem("Save", True).IsEnabled = True
                        ribtab.FindItem("Lock", True).IsEnabled = False
                        ribtab.FindItem("Unlock", True).IsEnabled = True
                        ribtab.FindItem("Exit", True).IsEnabled = True
                    Else
                        ribtab.FindItem("Save", True).IsEnabled = False
                        ribtab.FindItem("Lock", True).IsEnabled = True
                        ribtab.FindItem("Unlock", True).IsEnabled = False
                        ribtab.FindItem("Exit", True).IsEnabled = True
                    End If
                Else
                    MsgBox("No Connection settings recorded")
                End If
            End If
        End Sub

        Public Sub UpdateRibbonButtons()

            Dim GetParam As New ComINI
            Dim folder As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData

            Dim DocID As String
            Dim DocLockedStatus As Integer

            ArasUrl = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASURL")
            ArasDB = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASDB")
            ArasUser = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUN")
            ArasPass = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "ARASUP")
            Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
            DocID = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.UserData("Document ID")
            DocLockedStatus = ConnecAras.IsDocLocked(DocID)

            If DocLockedStatus = 0 Then
                ribtab.FindItem("Save", True).IsEnabled = False
                ribtab.FindItem("Lock", True).IsEnabled = True
                ribtab.FindItem("Unlock", True).IsEnabled = False
                ribtab.FindItem("Exit", True).IsEnabled = True

            Else
                ribtab.FindItem("Save", True).IsEnabled = True
                ribtab.FindItem("Lock", True).IsEnabled = False
                ribtab.FindItem("Unlock", True).IsEnabled = True
                ribtab.FindItem("Exit", True).IsEnabled = True
            End If

        End Sub

        Private Function CreatePDF() As Boolean
            Dim Result As Boolean = False




            'Create a new plot configuration with all needed parameters
            PtObj = ActiveDoc.Plot
            PtConfigs = ActiveDoc.PlotConfigurations


            'Add a new plot configuration
            PtConfigs.Add("PDF", False)

            'The plot config you created become active
            PlotConfig = PtConfigs.Item("PDF")

            'Use this method to set the scale
            PlotConfig.StandardScale = Autodesk.AutoCAD.Interop.Common.AcPlotScale.acScaleToFit

            'Updates the plot
            PlotConfig.RefreshPlotDeviceInfo()

            'Here you specify the pc3 file you want to use
            PlotConfig.ConfigName = "DWG To PDF.pc3"

            'You can select the plot style table here
            'PlotConfig.StyleSheet = "C:\Program Files\Autodesk\AutoCAD 2011\UserDataCache\Plotters\Plot Styles\acad.ctb"

            'Specifies whether or not to plot using the plot styles
            PlotConfig.PlotWithPlotStyles = True

            'If you are going to create pdf files in a batch mode,
            'I would recommend to turn off the BACKGROUNDPLOT system variable,
            'so autocad will not continue to do anything until finishes

            'the pdf creation
            ActiveDoc.SetVariable("BACKGROUNDPLOT", 0)

            'Updates the plot
            PlotConfig.RefreshPlotDeviceInfo()

            Try
                'Now you can use the PlotTofile method
                If PtObj.PlotToFile(Replace(ActiveDoc.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then
                    MsgBox("PDF Was Created")
                    Result = True

                Else
                    MsgBox("PDF Creation Unsuccessful")
                End If
            Catch ex As System.Exception

                MsgBox("error from Autocad plot config: DWG To PDF.pc3")
            End Try


            'If you wish you can delete th plot configuration you created
            'programmatically, and set the 'BACKGROUNDPLOT' system variable
            'to its original status.
            PtConfigs.Item("PDF").Delete()
            Return Result
        End Function

        Private Function CreateDWF() As Boolean
            Dim Result As Boolean = False




            'Create a new plot configuration with all needed parameters
            PtObj = ActiveDoc.Plot
            PtConfigs = ActiveDoc.PlotConfigurations

            'Add a new plot configuration
            PtConfigs.Add("DWF", False)

            'The plot config you created become active
            PlotConfig = PtConfigs.Item("DWF")

            'Use this method to set the scale
            PlotConfig.StandardScale = Autodesk.AutoCAD.Interop.Common.AcPlotScale.acScaleToFit

            'Updates the plot
            PlotConfig.RefreshPlotDeviceInfo()

            'Here you specify the pc3 file you want to use
            PlotConfig.ConfigName = "DWF6 ePlot.pc3"

            'You can select the plot style table here
            'PlotConfig.StyleSheet = "C:\Program Files\Autodesk\AutoCAD 2011\UserDataCache\Plotters\Plot Styles\acad.ctb"

            'Specifies whether or not to plot using the plot styles
            PlotConfig.PlotWithPlotStyles = True

            'If you are going to create pdf files in a batch mode,
            'I would recommend to turn off the BACKGROUNDPLOT system variable,
            'so autocad will not continue to do anything until finishes

            'the pdf creation

            ActiveDoc.SetVariable("BACKGROUNDPLOT", 0)

            'Updates the plot
            PlotConfig.RefreshPlotDeviceInfo()

            'Now you can use the PlotTofile method
            Try
                If PtObj.PlotToFile(Replace(ActiveDoc.FullName, "dwg", "dwf"), PlotConfig.ConfigName) Then
                    MsgBox("DWF Was Created")
                    Result = True

                Else
                    MsgBox("DWF Creation Unsuccessful")
                End If
            Catch ex As System.Exception

                MsgBox("error from Autocad plot config: DWF6 ePlot.pc3")
            End Try
           

            'If you wish you can delete th plot configuration you created
            'programmatically, and set the 'BACKGROUNDPLOT' system variable
            'to its original status.
            PtConfigs.Item("DWF").Delete()

            Return Result
        End Function

        Private Function CreatePNG_Thumbnail() As Boolean

            Dim Result As Boolean = False


            'Create a new plot configuration with all needed parameters
            PtObj = ActiveDoc.Plot
            PtConfigs = ActiveDoc.PlotConfigurations


            'Add a new plot configuration

            PtConfigs.Add("PNG")



            'The plot config you created become active
            PlotConfig = PtConfigs.Item("PNG")


            'Here you specify the pc3 file you want to use
            PlotConfig.ConfigName = "PublishThumbnail.pc3"


            'Specifies whether or not to plot using the plot styles
            PlotConfig.PlotWithPlotStyles = True
            'Updates the plot
            PlotConfig.RefreshPlotDeviceInfo()


            'If you are going to create pdf files in a batch mode,
            'I would recommend to turn off the BACKGROUNDPLOT system variable,
            'so autocad will not continue to do anything until finishes

            'the pdf creation
            Dim plot_back As Integer = ActiveDoc.GetVariable("BACKGROUNDPLOT")
            ActiveDoc.SetVariable("BACKGROUNDPLOT", 0)

            'Updates the plot
            PlotConfig.RefreshPlotDeviceInfo()

            'Now you can use the PlotTofile method
            Try
                If PtObj.PlotToFile(Replace(ActiveDoc.FullName, "dwg", "png"), PlotConfig.ConfigName) Then
                    MsgBox("Thumbnail Was Created")
                    Result = True

                Else
                    MsgBox("Thumbnail Creation Unsuccessful")
                End If
            Catch ex As System.Exception
                MsgBox(ex.Message)
                MsgBox("error from Autocad plot config: PublishToWeb PNG.pc3")

            End Try


            'If you wish you can delete th plot configuration you created
            'programmatically, and set the 'BACKGROUNDPLOT' system variable
            'to its original status.
            PtConfigs.Item("PNG").Delete()
            PtObj = Nothing
            PlotConfig = Nothing
            ActiveDoc.SetVariable("BACKGROUNDPLOT", plot_back)

            Return Result
        End Function



        Public Function createpdf2()
            Dim Result As Boolean = False
            Dim BackPlot As Object

            'Create a new plot configutrarion with all needed parameters
            PtObj = ActiveDoc.Plot
            PtConfigs = ActiveDoc.PlotConfigurations
            'Add a new plot configuration
            PtConfigs.Add("PDF", False)
            'The plot config you created become active
            PlotConfig = PtConfigs.Item("PDF")
            'Use this method to set the scale
            PlotConfig.StandardScale = Autodesk.AutoCAD.Interop.Common.AcPlotScale.acScaleToFit

            'Updates the plot
            PlotConfig.RefreshPlotDeviceInfo()
            'Here you specify the pc3 file you want to use
            PlotConfig.ConfigName = "DWG To PDF.pc3"
            'You can select the plot style table here
            'PlotConfig.StyleSheet = "Acad.ctb"
            'Specifies whether or not to plot using the plot styles
            PlotConfig.PlotWithPlotStyles = True

            'If you are going to create pdf files in a batch mode,
            'I would recommend to turn off the BACKGROUNDPLOT system variable,
            'so autocad will not continue to do anything until finishes
            'the pdf creation
            BackPlot = ActiveDoc.GetVariable("BACKGROUNDPLOT")
            ActiveDoc.SetVariable("BACKGROUNDPLOT", 0)
            'Updates the plot
            PlotConfig.RefreshPlotDeviceInfo()
            'Now you can use the PlotTofile method
            If PtObj.PlotToFile(Replace(ActiveDoc.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then
                MsgBox("PDF Was Created")
                Result = True
            Else
                MsgBox("PDF Creation Unsuccessful")
                Result = False
            End If
            'If you wish you can delete th plot configuration you created
            'programmatically, and set the 'BACKGROUNDPLOT' system variable
            'to its original status.
            PtConfigs.Item("PDF").Delete()
            PlotConfig = Nothing
            ActiveDoc.SetVariable("BACKGROUNDPLOT", BackPlot)
            Return Result
        End Function

    End Class

End Namespace